/**
 * 
 */
package userLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.ResultSet;

import constants.Constants;
import encryption.Encrypt;
import userEntity.CustomerEntity;
import userEntity.EmployeeEntity;
import userService.CreateUserService;
import validator.Validator;
import userService.CreateUserService;

/**
 * @author Yokesh Kumar
 *
 */
public class CreateUserLogic {

	/**
	 * This method checks whether the loginId entered by the
	 * user is already present in the login table
	 * @param LoginId
	 * @return
	 */
	public boolean checkLoginId(String LoginId){
		CreateUserService service = new CreateUserService();
		ResultSet rs = null;
		try {
			System.out.println(LoginId);
			rs = service.checkLoginId(LoginId);
			if(rs!= null && rs.next()){
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return true;
	}
	
	/**
	 * This method checks whether the emailId entered is already present 
	 * in user table
	 * @param EmailId
	 * @return
	 */
	public boolean checkEmailId(String EmailId){
		CreateUserService service = new CreateUserService();
		ResultSet rs = null;
		try {
			System.out.println(EmailId);
			rs = service.checkEmailId(EmailId);
			if(rs!= null && rs.next()){
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return true;
	}
	
	
	
	/**
	 * This method checks whether the user is active or not
	 * @param phoneNumber
	 * @return
	 */
	public boolean checkUserStatus(String phoneNumber)
	{
		CreateUserService service = new CreateUserService();
		ResultSet rs = null;
		try {
			rs = service.checkUserStatus(phoneNumber);
			if(rs!= null && rs.next()){
				if(rs.getInt(5) == 1)
				{
					return true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return false;
	}
	
	/**
	 * This method checks whether user is active or not
	 * @param loginId
	 * @return
	 */
	public boolean checkStatus(String loginId)
	{
		CreateUserService service = new CreateUserService();
		ResultSet rs = null;
		try {
			rs = service.checkStatus(loginId);
			if(rs!= null && rs.next()){
				if(rs.getInt(5) == 1)
				{
					return true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return false;
	}
	
	/**
	 * This method checks whether phone number is already present
	 * @param phoneNumber
	 * @return
	 */
	public boolean checkPhoneNumber(String phoneNumber){
		CreateUserService service = new CreateUserService();
		ResultSet rs = null;
		try {
			rs = service.checkPhonenumber(phoneNumber);
			if(rs!= null && rs.next()){
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		service.closeConnection();
		return true;
	}

	/**
	 * This method checks whether the phoneNumber entered by the
	 * user is already present in the user table
	 * @param phoneNumber
	 * @return
	 */
	public String validateAndCheckPhNo(String phoneNumber){
		CreateUserService service = new CreateUserService();
		String message = "";
		if(Validator.validateStringContainsInteger(phoneNumber)){
			if(Validator.validateLength(phoneNumber, 10)){
				String code = "+1";
				phoneNumber = code.concat(phoneNumber);
				if(!checkPhoneNumber(phoneNumber)){
					message = "User with this phoneNumber already exist";
				}
			}
			else{
				message ="Phone number cannot be more than 10 numbers";
			}
		}
		else{
			message ="Phone number can contain only numbers";
		}
		service.closeConnection();
		return message;
	}


	/**
	 * This method calls service to store the details entered by the user
	 * @param user
	 * @return
	 * @throws Exception 
	 */
	public boolean createCustomer(CustomerEntity customer) throws Exception{
		CreateUserService service = new CreateUserService();
		customer.setUserType(Constants.CUSTOMER);
		customer.setCustomerType(Constants.CUSTOMER_REGISTERED);
		customer.setMember(Constants.NOT_MEMBER);
		customer.setRole(Constants.CUSTOMER);
		
		try {
			customer.setPwd(Encrypt.encrypt(customer.getPwd()));
			service.createCustomer(customer);
			service.closeConnection();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			service.closeConnection();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			service.closeConnection();
			throw e;
		}
		
		
	}

	/**
	 * This method calls service to store the details about the employee
	 * entered by the admin
	 * @param employee
	 * @return
	 * @throws Exception 
	 */
	public boolean createEmployee(EmployeeEntity employee) throws Exception {
        
		CreateUserService service = new CreateUserService();
		try {
			employee.setPwd(Encrypt.encrypt(employee.getPwd()));
			service.createEmployee(employee);
			service.closeConnection();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			service.closeConnection();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			service.closeConnection();
			throw e;
		}
		
	}






}
